<template>
  <div class="child2">
    <h3>子组件2</h3>
    <h4>电脑：{{ computer }}</h4>
    <h4 v-show="toy2">弟弟得到了哥哥的玩具：{{ toy2 }}</h4>
    <button @click="emitter.emit('get-computer', computer)">
      把电脑送给父亲
    </button>
  </div>
</template>

<script setup lang="ts" name="Child2">
//子组件2接收数据
import { ref, onUnmounted } from "vue";
import emitter from "../../utils/emitter";

let toy2 = ref();
// 数据
let computer = ref("联想");
emitter.on("send-toy", (value) => {
  console.log("send-toy", value);
  toy2.value = value;
});

//在组件卸载时，解绑事件
onUnmounted(() => {
  emitter.off("send-toy");
});
</script>

<style scoped>
.child2 {
  margin-top: 50px;
  background-color: orange;
  padding: 10px;
  box-shadow: 0 0 10px black;
  border-radius: 10px;
}
</style>